package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.OrderStartDriveEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OrderStartDriveEventListener implements ApplicationListener<OrderStartDriveEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(OrderStartDriveEventListener.class);

    @Autowired
    private CompanyService companyService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SettingUtils settingUtils;


    @Override
    @Async
    public void onApplicationEvent(OrderStartDriveEvent event) {

        Order order = (Order) event.getSource();
        Passenger passenger = memberService.findOnePassenger(order.getClientId());

        logger.debug("order arriveplace event:{}", order.getOrderNumber());

        Driver driver = driverService.findOne(order.getDriverId());

        if (null != driver) {
            //推送数据到浏览器
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("driverID", driver.getId());
            jsonObject.put("isworkCar", driver.isWorkCar());
            jsonObject.put("type", "startdrive");
            Company mainCompany = companyService.getMain();
            Company orderCompany = companyService.findOne(order.getCompanyId());
            WebSocketSend
                .boardcastToArea(jsonObject, orderCompany.getCityName(), mainCompany.getId());
        }

        if (null != passenger) {
            //给客户推送
            passenger.setCid(memberService.findCid(passenger.getId()));
            if (StringUtils.isNotBlank(passenger.getCid())) {
                try {
                    //发送设备推送
                    PushData pushData = new PushData();
                    pushData.setType("RUN_ORDER");
                    pushData.setTitle("代驾订单开始执行");
                    pushData.setDescription("服务人员已经开始执行您的订单,单号:" + order.getOrderNumber());
                    pushData.setData(order.getId());

                    GeTuiMember.push(passenger, pushData, settingUtils.get());
                } catch (IOException e) {
                    logger.debug("push error : " + e.getMessage());
                }
            }
        }


    }

}
